﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Quartz;
using Common.Logging;
using System.Collections;
using Common.Model;
using BLL;
namespace stock
{
    public class SimpleQuartzJob : IStatefulJob
    {
        private ILog log = LogManager.GetLogger(typeof(SimpleQuartzJob));
        private int count = 1;
        public void Execute(JobExecutionContext context)
        {
            try
            {
                String jobName = context.JobDetail.Name;
                int i = (int)context.JobDetail.JobDataMap["job"];
                int count = context.JobDetail.JobDataMap.GetInt("job");
                StockJob();
                count++;
                context.JobDetail.JobDataMap.Put("job", count);
                log.Info("触发器第" + i.ToString() + "次触发，任务" + jobName + "完成！");
            }
            catch (Exception e)
            {
                log.Info("触发器任务" + context.JobDetail.Name + "失败！");
                JobExecutionException ex = new JobExecutionException(e);
                ex.RefireImmediately = true;
                throw ex;
            }
        }

        public void StockJob()
        {
            Hashtable stockHash = new Hashtable();
            List<Common.Model.StockVirtual> Virtuals = (new BLL.Tools()).GetAll();
            if (Virtuals != null && Virtuals.Count > 0)
            {
                foreach (Common.Model.StockVirtual virtualModel in Virtuals)
                {
                    stockHash[virtualModel.StockCode] = virtualModel;
                }
                globalObject.GlobalHash = stockHash;
                //判断用户读取定义数据和现实数据的差异，选择发送邮件
                BLL.StockAlertBLL alertBLL = new BLL.StockAlertBLL();
                List<Common.Model.StockAlert> AlertList = alertBLL.GetModelListByCache("");//以后这个数据应该被存储在cache中
                foreach (Common.Model.StockAlert AlertModel in AlertList)
                {
                    Common.Model.StockVirtual tempVirtual = (Common.Model.StockVirtual)stockHash[AlertModel.Code];
                    if (tempVirtual.UpDownMargin >= 0)//涨
                    {
                        if (DayOfSub(AlertModel.UPChangeTime))
                        {
                            if (AlertModel.UpAlertMargin*100 < (decimal)tempVirtual.UpDownMargin)//达到提醒标准
                            {//提醒，然后更改时间和提醒价格 0.02 2%   2 2%    
                                StockTiXing(alertBLL, AlertModel, tempVirtual, 1);
                            }
                            else if (AlertModel.UpAlertPrice < (decimal)tempVirtual.XianJia)//达到提醒标准
                            {//提醒
                                StockTiXing(alertBLL, AlertModel, tempVirtual, 2);
                            }
                        }
                        else
                        {
                            if (AlertModel.UpAlertPrice < (decimal)tempVirtual.XianJia)//达到提醒标准
                            {//提醒
                                StockTiXing(alertBLL, AlertModel, tempVirtual, 2);
                            }
                        }
                    }
                    else//跌
                    {
                        if (DayOfSub(AlertModel.DownChangeTime))
                        {
                            if (AlertModel.DownAlertMargin*100 > (decimal)tempVirtual.UpDownMargin)
                            {//提醒
                                StockTiXing(alertBLL, AlertModel, tempVirtual, 3);
                            }
                            else if (AlertModel.DownAlertPrice > (decimal)tempVirtual.XianJia)
                            {//提醒
                                StockTiXing(alertBLL, AlertModel, tempVirtual, 4);
                            }
                        }
                        else
                        {
                            if (AlertModel.DownAlertPrice > (decimal)tempVirtual.XianJia)
                            {//提醒
                                StockTiXing(alertBLL, AlertModel, tempVirtual, 4);
                            }
                        }
                    }
                }
            }            
        }
        //提醒
        private void StockTiXing(BLL.StockAlertBLL alertBLL, Common.Model.StockAlert AlertModel, Common.Model.StockVirtual tempVirtual, int p)
        {
            Common.Model.Customer user;
            user = (new BLL.CustomerBLL()).GetModel(AlertModel.CustomerId);
            String body = "";
            switch (p)
            {
                case 1:
                    AlertModel.UPChangeTime = DateTime.Now;
                    AlertModel.UpAlertPrice = tempVirtual.XianJia + AlertModel.UpAuto;
                    body = user.Nick + "您好，" + tempVirtual.StockName + "波动" + tempVirtual.UpDownMargin + "%,现在：" + tempVirtual.XianJia;
                    BLL.Tools.SendMail(user.Email, body,tempVirtual.StockName+"新情况", null, false);
                    break;
                case 2:
                    AlertModel.UPChangeTime = DateTime.Now;
                    AlertModel.UpAlertPrice = tempVirtual.XianJia + AlertModel.UpAuto;
                    body = user.Nick + "您好，" + tempVirtual.StockName + "波动" + tempVirtual.UpDownMargin + "%,现在：" + tempVirtual.XianJia;
                    BLL.Tools.SendMail(user.Email, body,tempVirtual.StockName+"新情况", null, false);
                    break;
                case 3:
                    AlertModel.DownChangeTime = DateTime.Now;
                    AlertModel.DownAlertPrice = tempVirtual.XianJia - AlertModel.DownAuto;
                    body = user.Nick + "您好，" + tempVirtual.StockName + "波动" + tempVirtual.UpDownMargin + "%,现在：" + tempVirtual.XianJia;
                    BLL.Tools.SendMail(user.Email, body,tempVirtual.StockName+"新情况", null, false);
                    break;
                case 4:
                    AlertModel.DownChangeTime = DateTime.Now;
                    AlertModel.DownAlertPrice = tempVirtual.XianJia - AlertModel.DownAuto;
                    body = user.Nick + "您好，" + tempVirtual.StockName + "波动" + tempVirtual.UpDownMargin + "%,现在：" + tempVirtual.XianJia;
                    BLL.Tools.SendMail(user.Email, body,tempVirtual.StockName+"新情况", null, false);
                    break;
            }
            alertBLL.Update(AlertModel);
        }

        private Boolean DayOfSub(DateTime Time)//为true的时候说明是昨天改的
        {
            TimeSpan nowSpan = new TimeSpan(DateTime.Now.Ticks);
            TimeSpan userSpan = new TimeSpan(Time.Ticks);
            TimeSpan subSpan = nowSpan.Subtract(userSpan);
            int day = subSpan.Days;
            if (day > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}